from pydantic import BaseModel, Field
from typing import Generic, List, TypeVar, Optional

T = TypeVar('T')

class Pageable(BaseModel):
    """分页请求参数模型"""
    page: int = Field(default=1, ge=1, description="当前页码，即要跳转的页码")
    pageSize: int = Field(default=20, ge=1, le=100, description="每页显示数")

    def to_skip_limit(self) -> tuple[int, int]:
        """转换为skip和limit参数
        
        Returns:
            tuple[int, int]: (skip, limit) 用于SQLAlchemy查询
        """
        skip = (self.page - 1) * self.pageSize
        limit = self.pageSize
        return skip, limit

class Page(BaseModel, Generic[T]):
    """分页响应模型"""
    items: List[T] = Field(..., description="查询返回的结果集")
    currentPage: int = Field(default=1, description="当前页码，即要跳转的页码")
    pageSize: int = Field(default=20, description="每页显示数")
    totalCount: int = Field(default=-1, description="数据的总数（？条）")
    totalPage: int = Field(default=-1, description="总页数")
    
    model_config = {
        "arbitrary_types_allowed": True
    }
    
    @classmethod
    def create(cls, items: List[T], currentPage: int, pageSize: int, totalCount: int) -> 'Page[T]':
        """创建分页响应对象
        
        Args:
            items: 查询返回的结果集
            currentPage: 当前页码，即要跳转的页码
            pageSize: 每页显示数
            totalCount: 数据的总数（？条）
            
        Returns:
            Page: 分页响应对象
        """
        total_page = (totalCount + pageSize - 1) // pageSize  # 向上取整
        return cls(
            items=items,
            currentPage=currentPage,
            pageSize=pageSize,
            totalCount=totalCount,
            totalPage=total_page
        )